package com.mkl.system.controller;

import com.mkl.system.common.DataGridView;
import com.mkl.system.common.ResultObj;
import com.mkl.system.service.LoginfoService;
import com.mkl.system.vo.LoginfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author king
 * @version 1.0
 * @date 2020/3/4 15:47
 */
@RestController
@RequestMapping("api/loginfo")
//@CrossOrigin
public class LoginfoController {
    @Autowired
    private LoginfoService loginfoService;

    /**
     * 记载所有数据，根据条件模糊查询加载数据
     *
     * @param loginfoVo loginfoVo
     * @return 返回表格结果
     */
    @RequestMapping("loadAllLoginfo")
    public DataGridView loadAllLoginfo(LoginfoVo loginfoVo) {
        return loginfoService.queryAllLoginfo(loginfoVo);
    }

    /**
     * 根据单个id删除日志
     *
     * @param id 日志id
     * @return 返回结果
     */
    @RequestMapping("deleteLoginfo")
    public ResultObj deleteLoginfo(Integer id) {
        try {
            loginfoService.removeById(id);
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

    /**
     * 根据多个id批量删除日志
     *
     * @param ids ids
     * @return 返回结果
     */
    @RequestMapping("batchDeleteLoginfo")
    public ResultObj batchDeleteLoginfo(Integer[] ids) {
        try {
            if (null != ids && ids.length > 0) {
                List<Integer> idsList = new ArrayList<>();
                Collections.addAll(idsList, ids);
                loginfoService.removeByIds(idsList);
                return ResultObj.DELETE_SUCCESS;
            } else {
                return new ResultObj(-1, "删除数据不能为空！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }

    }


}
